package com.qqtop10.manager.controller;

import java.io.UnsupportedEncodingException;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.qqtop10.manager.model.Category;
import com.qqtop10.manager.model.PagingArgs;
import com.qqtop10.manager.model.Perm;
import com.qqtop10.manager.model.Role;
import com.qqtop10.manager.model.Result;
import com.qqtop10.manager.service.RoleService;

/**
 * 
 * @ClassName: RoleController
 * @Description: RoleController
 * 
 */
@RequestMapping("/manager/role/")
@Controller
public class RoleController extends BaseController {
	@Autowired
	private RoleService roleService;

	@RequestMapping("all.do")
	@ResponseBody
	public Result all(Role input) {
		try {
			PagingArgs pagingArgs = getPagingArgs();
			int count = roleService.count(input);
			List<Role> array = roleService.all(input, pagingArgs);
			return page(count, array);
		} catch (Exception e) {
			return excep(e);
		}
	}

	@RequestMapping("addRole.do")
	@ResponseBody
	public Result AddRole(HttpServletRequest request, HttpServletResponse response,Role input) throws UnsupportedEncodingException {
		request.setCharacterEncoding("UTF-8");
		response.setCharacterEncoding("UTF-8");
		String name = (input.getName());
		input.setName(name);
		String codes = (input.getPerm_codes());
		input.setPerm_codes(codes);
		input.setUuid(newId());
		input.setCreatetime(now());
		input.setCreator(getLogindUuid());
		int res = roleService.insert(input);
		if (res > 0) {
			return ok("操作成功");
		} else {
			return err("添加失败");
		}
	}

	@RequestMapping("updateRole.do")
	@ResponseBody
	public Result UpdateRebateLink(HttpServletRequest request, HttpServletResponse response,Role input) {
		String name = (input.getName());
		input.setName(name);
		String codes = (input.getPerm_codes());
		input.setPerm_codes(codes);
		input.setUpdatetime(now());
		input.setMender(getLogindUuid());
		int ret = roleService.update(input);
		if (ret > 0) {
			return ok("更新成功");
		} else {
			return err("操作失败");
		}
	}

	@RequestMapping("deleteById.do")
	@ResponseBody
	public Result DeleteById(int ids) {
		int ret = roleService.delete(ids);
		if (ret > 0) {
			return ok("删除成功");
		} else {
			return err("操作失败");
		}
	}

	@RequestMapping("getRoleById.do")
	@ResponseBody
	public ModelMap GetRebateLinkById(int id) {
		ModelMap map = new ModelMap();
		Role data = roleService.selectById(id);
		if (data == null) {
			map.put("data", "暂无信息");
		} else {
			map.put("data", data);
		}
		return map;
	}
	
	@RequestMapping("treePerm.do")
	@ResponseBody
	public Result treePerm(Perm input) {
		try {
			PagingArgs pagingArgs = new PagingArgs();
			pagingArgs.set_limit(Integer.MAX_VALUE);
			return ok(roleService.allPerm(input, pagingArgs));
		} catch (Exception e) {
			return excep(e);
		}
	}
	
	@RequestMapping("treeRole.do")
	@ResponseBody
	public Result treeRole(Role input) {
		try {
			PagingArgs pagingArgs = new PagingArgs();
			pagingArgs.set_limit(Integer.MAX_VALUE);
			return ok(roleService.all(input, pagingArgs));
		} catch (Exception e) {
			return excep(e);
		}
	}
}
